package com.ddwl.message.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.message.service.SosService;
import com.ddwl.schema.bo.base.LocationBo;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.message.InformerBo;
import com.ddwl.schema.bo.message.SosBo;
import com.ddwl.schema.bo.message.SosPageBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.message.SosVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/8/8 14:49
 */
@Api(tags = "守护服务")
@RequestMapping("sos")
@RestController
@RequiredArgsConstructor
public class SosController extends BaseController {

    private final SosService sosService;

    @PostMapping("/alarm")
    @Operation(summary = "设备报警")
    @Auth
    public RestVo alarm(@RequestBody SosBo bo) {
        fillBaseField(bo);
        return RestVo.ok();
    }

    @PostMapping("/create")
    @Operation(summary = "创建互帮")
    @Auth
    public RestVo create(@Valid @RequestBody SosBo bo) {
        fillBaseField(bo);
        bo.setUserId(getUserId());
        sosService.create(bo);
        return RestVo.ok();
    }

    @PostMapping("/update")
    @Operation(summary = "修改互帮")
    @Auth
    public RestVo update(@Valid @RequestBody SosBo bo) {
        fillBaseField(bo);
        sosService.update(bo);
        return RestVo.ok();
    }

    @PostMapping("/audit")
    @Operation(summary = "审核互帮")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo audit(@Valid @RequestBody SosBo bo) {
        fillBaseField(bo);
        sosService.audit(bo);
        return RestVo.ok();
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除互帮")
    @Auth
    public RestVo delete(@RequestParam String id) {
        sosService.delete(id);
        return RestVo.ok();
    }

    @PostMapping("/informer")
    @Operation(summary = "提供线索")
    @Auth
    public RestVo informer(@Valid @RequestBody InformerBo bo) {
        bo.setUserId(getUserId());
        sosService.informer(bo);
        return RestVo.ok();
    }

    @PostMapping("/resolve")
    @Operation(summary = "互帮已解决")
    @Auth
    public RestVo resolve(@RequestBody InformerBo bo) {
        sosService.resolve(bo);
        return RestVo.ok();
    }

    @GetMapping("/detail")
    @Operation(summary = "获取互帮明细")
    @Auth
    public RestVo<SosVo> detail(@RequestParam String id) {
        return RestVo.ok(sosService.getById(id));
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询互帮")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<PageVo<SosVo>> page(@ApiParam(value = "分页Bo", required = true) @Valid @RequestBody PageBo<SosPageBo> pageBo) {
        return RestVo.ok(sosService.page(pageBo));
    }

    @PostMapping("/near")
    @Operation(summary = "附近互帮")
    @Auth
    public RestVo<List<SosVo>> near(@ApiParam(value = "定位", required = true) @Valid @RequestBody LocationBo loc) {
        return RestVo.ok(sosService.near(loc));
    }

}
